Python

How to create custom 404 error page in Django?

How to create custom 404 error page in Django?, someone asked me to explain?

In this tutorial I will show you how to create custom 404 error page for a Django project.

Creating a custom 404 page provides a user-friendly informative message when a page is not found. Instead of a "Page Not Found" message,  show your custom 404 page.

Solution:

1.Ensure that Django project is configured to use custom error pages by setting DEBUG = False in settings.py.

2.In templates directory, create a file named 404.html.

Example 404 Page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Page Not Found</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 50px;
        }
        h1 {
            font-size: 3em;
            color: #ff6f61;
        }
        p {
            font-size: 1.5em;
        }
        a {
            color: #007bff;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>404 - Page Not Found</h1>
    <p>Sorry, the page you are looking for does not exist.</p>
    <a href="/">Return to Home</a>
</body>
</html>

VIDEO GUIDE:

Post your comments / questions